package ar.edu.itba.pod.legajo48240.pruebas;

import java.util.Properties;

import org.apache.log4j.Logger;
import org.joda.time.Duration;

import ar.edu.itba.pod.agent.market.Consumer;
import ar.edu.itba.pod.agent.market.Market;
import ar.edu.itba.pod.agent.market.Producer;
import ar.edu.itba.pod.agent.market.Resource;
import ar.edu.itba.pod.legajo48240.model.Host;

public class PruebaNico implements Runnable {

	static Logger LOGGER = Logger.getLogger(PruebaNico.class);

	static Properties constants = new Properties();

	public static void main(String[] args) {
		new PruebaNico().run();
	}

	@Override
	public void run() {
		
		/**
		 * 			ATENCION !
		 * 		===============
		 * 		Cambiar el parametro IP de la maquina virtual !
		 * 		en run configuration
		 * 
		 */
		String host = "10.6.0.33";
		
		String maru = "10.6.0.114";
//		String jose = "192.168.1.208";
//		String cris = "192.168.1.207";
		String dani = "10.6.0.92";
		String nico = "10.6.0.107";
//		
		int maru_port =1002;
//		int cris_port =1099;
		int jose_port =1099;
		int dani_port =1099;
		
		
		boolean WITH_EVENTS = true;
		boolean START_SIMULATION = false;
		
		int SECONDS=4;

//		Host hostA = new Host(jose, jose_port, jose);
		
//		Host hostA = new Host(host, 7000, "10");
//		Host hostB = new Host(host, 7001, "15");
//		Host hostC = new Host(host, 7002, "30");
//		Host hostD = new Host(host, 7003, "23");
//		Host hostE = new Host(host, 7004, "50");
//		
		Host hostA = new Host(host, jose_port, host+":" +jose_port);
//		Host hostB = new Host(host, 7001, "B");
//		Host hostC = new Host(host, 7002, "C");
//		Host hostD = new Host(host, 7003, "D");
//		Host hostE = new Host(host, 7004, "E");

		
		try {
//			hostA.network().createGroup();
			hostA.network().connectToGroup(maru,maru_port);
			try {

				Resource gold = new Resource("Mineral", "Gold");
				Resource copper = new Resource("Mineral", "Copper");
				Resource steel = new Resource("Alloy", "Steel");
				
				LOGGER.info("Agrego agentes...");
				
				hostA.simulation().add(new Producer( "gold mine " , gold, Duration.standardDays(3000), 1));
				
				LOGGER.info("AGents on A: "  + hostA.simulation().getAgentsRunning());
				
				hostA.simulation().add(new Producer( "steel mine" , steel,Duration.standardDays(1), 2));
				
				LOGGER.info("AGents on A: "  + hostA.simulation().getAgentsRunning());
				
				hostA.simulation().add(new Producer( "copper mine" , copper,	Duration.standardDays(1), 10));
				
				LOGGER.info("AGents on A: "  + hostA.simulation().getAgentsRunning());
				
				hostA.simulation().add(new Producer( "gold mine F" , gold, Duration.standardDays(3000), 1));
				
				LOGGER.info("AGents on A: "  + hostA.simulation().getAgentsRunning());
				
				hostA.simulation().add(new Producer( "steel mine F" , steel,Duration.standardDays(1), 2));
				
				LOGGER.info("AGents on A: "  + hostA.simulation().getAgentsRunning());
				
				hostA.simulation().add(new Producer( "copper mine F" , copper,	Duration.standardDays(1), 10));
				
				LOGGER.info("AGents on A: "  + hostA.simulation().getAgentsRunning());
				
//				try { Thread.sleep(SECONDS*1000); } catch (InterruptedException ignore) {}
//				hostB.network().connectToGroup(hostA.host(), hostA.port());
				
				
//				try { Thread.sleep(SECONDS*1000); } catch (InterruptedException ignore) {}
//				hostD.network().connectToGroup(hostA.host(), hostA.port());
				
//				try { Thread.sleep(SECONDS*1000); } catch (InterruptedException ignore) {}
//				hostC.network().connectToGroup(hostD.host(), hostD.port());
				
//				try { Thread.sleep(SECONDS*1000); } catch (InterruptedException ignore) {}
//				hostE.network().connectToGroup(hostB.host(), hostB.port());
				
				
				try { Thread.sleep(1*1000); } catch (InterruptedException ignore) {}
				
				
				
				LOGGER.info("CLUSTER CREATED SUCCESFULLY -- Coordinator: " + hostA.getCoordinator() +" in " + hostA.id());
//				LOGGER.info("CLUSTER CREATED SUCCESFULLY -- Coordinator: " + hostB.getCoordinator() +" in " + hostB.id());
//				LOGGER.info("CLUSTER CREATED SUCCESFULLY -- Coordinator: " + hostC.getCoordinator() +" in " + hostC.id());
//				LOGGER.info("CLUSTER CREATED SUCCESFULLY -- Coordinator: " + hostD.getCoordinator() +" in " + hostD.id());
//				LOGGER.info("CLUSTER CREATED SUCCESFULLY -- Coordinator: " + hostE.getCoordinator() +" in " + hostE.id());
				

				if(WITH_EVENTS){
					

					for (int i = 0; i < 1; i++) {
						
//						hostA.simulation().add(new Producer( "gold mine" + i, gold, Duration.standardDays(3000), 1));
//						
//						hostA.simulation().add(new Producer( "steel mine" + i, steel,Duration.standardDays(1), 2));
//						
//						hostA.simulation().add(new Producer( "copper mine" + i, copper,	Duration.standardDays(1), 10));
						
//						hostB.simulation().add(new Consumer( "gold consumer" + i, gold,	Duration.standardDays(4000), 4));
//						
//						
//						hostB.simulation().add(new Consumer( "steel consumer" + i, steel, Duration.standardDays(3), 2));
//						
//						hostC.simulation().add(new Consumer( "copper consumer" + i, copper,	Duration.standardHours(8), 2));
					}
					
//					hostC.simulation().add(new Market( "gold market", gold));
//					hostA.simulation().add(new Market( "cooper market", copper));
//					hostA.simulation().add(new Market( "steel market", steel));

//					try { Thread.sleep(1*1000); } catch (InterruptedException ignore) {}
//					hostD.simulation().stop();
					
					if(START_SIMULATION){
						LOGGER.info("SIMULATION >> Starting ........... .....................................\n");
						hostA.simulation().start(Duration.standardDays(10));
//						hostB.simulation().start(Duration.standardDays(10));
//						hostC.simulation().start(Duration.standardDays(10));
					}else{
						LOGGER.info("SIMULATION DISABLED");
					}
						
				}

			} catch (Exception e) {
				LOGGER.error("CLUSTER Network Connection problem");
				e.printStackTrace();
				
			} 
		}catch (Exception e) {
			LOGGER.error("CLUSTER NOT CREATED ! Maybe Already Started");
			e.printStackTrace();
		}finally{
//			hostA.finish();
//			hostB.finish();
//			hostC.finish();
		}
	}
}
